﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Futajma.MigrationStudio.LiveLinkImporter.DeploymentReport
{



    class ItemLoader
    {
        const string ItemWasDeplyedWithAdifferentName = "Item was deployed with a different name";
        const string ItemNotUploadedToSharePoint = "Item is of 0KB size, and might not be uploaded to SharePoint";
        const string URLExceeds255Characters = "URL exceeds 255 characters";

        Dictionary<string, Item> _items = new Dictionary<string, Item>();

        public string[] Keys
        {
            get
            {
                return _items.Keys.ToArray();
            }
        } 

        public Item this[string sourceUrl]
        {
            get
            {
                if (_items.ContainsKey(sourceUrl))
                {
                    return _items[sourceUrl];
                }
                else
                {
                    return null;
                }
            }
        }

        public void LoadReport(string filename)
        {
            _items.Clear();

            XDocument separateFileDoc = XDocument.Load(filename);

            var items = separateFileDoc.Descendants("Item");

            foreach (var xItem in items)
            {
                var item = new Item(xItem);

                if (item.Message.Contains(ItemWasDeplyedWithAdifferentName))
                {
                    _items.Add(item.SourceUrl, item);
                }
            }

        }
    }
}
